using System;
using System.Threading.Tasks;

namespace Pay.TO {
	/// <summary>
	/// 返回响应消息： result_code是错误代码，默认1000成功，其它失败；result_msg是返回响应消息 data是业绩结果：可能无；
	/// </summary>
	[Serializable]
	public class ResultMessage<T> {
		private int _result_code = 1000;
		/// <summary>
		/// 默认1000表示成功，其它失败
		/// </summary>
		public int result_code {
			get { return _result_code; }
			set { _result_code = value; }
		}
		/// <summary>
		/// 返回执行结果消息
		/// </summary>
		public string result_msg { get; set; }
		/// <summary>
		/// data是业绩结果：可能无
		/// </summary>
		public T data { get; set; }
		/// <summary>
		/// 返回响应消息： result_code是错误代码，默认1000成功，其它失败；result_msg是返回响应消息 data是业绩结果：可能无；
		/// </summary>
		public ResultMessage() { }
		public ResultMessage(string msg, T data = default(T)) {
			result_msg = msg;
			this.data = data;
		}
		public ResultMessage(int code, string msg, T data = default(T)) {
			result_code = code;
			result_msg = msg;
			this.data = data;
		}
	}
	public class ResultToJson {
		/// <summary>
		/// 成功返回
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="data"></param>
		/// <returns></returns>
		public static ResultMessage<T> Json<T>(T data = default(T)) {
			return new ResultMessage<T> {
				result_msg = "",
				data = data
			};
		}
		/// <summary>
		/// 成功返回
		/// </summary>
		/// <returns>The async.</returns>
		/// <param name="data">Data.</param>
		/// <typeparam name="T">The 1st type parameter.</typeparam>
		public static async Task<ResultMessage<T>> JsonAsync<T>(T data = default(T)) {
			Task<ResultMessage<T>> t1 = Task.Factory.StartNew(() =>
			  new ResultMessage<T> {
				  result_msg = "",
				  data = data
			  });
			return await t1;
		}
		/// <summary>
		/// 成功返回
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="msg"></param>
		/// <param name="data"></param>
		/// <returns></returns>
		public static ResultMessage<T> Json<T>(string msg, T data = default(T)) {
			return new ResultMessage<T> {
				result_msg = msg,
				data = data
			};
		}
		/// <summary>
		/// 成功返回
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="msg"></param>
		/// <param name="data"></param>
		/// <returns></returns>
		public static async Task<ResultMessage<T>> JsonAsync<T>(string msg, T data = default(T)) {
			Task<ResultMessage<T>> t1 = Task.Factory.StartNew(() =>
			 new ResultMessage<T> {
				 result_msg = msg,
				 data = data
			 });
			return await t1;
		}
		/// <summary>
		/// 失败返回
		/// </summary>
		/// <param name="msg"></param>
		/// <returns></returns>
		public static ResultMessage<T> Json<T>(string msg) {
			return new ResultMessage<T> {
				result_msg = msg,
				result_code = 0
			};
		}
		/// <summary>
		/// 失败返回
		/// </summary>
		/// <param name="msg"></param>
		/// <returns></returns>
		public static async Task<ResultMessage<T>> JsonAsync<T>(string msg) {
			Task<ResultMessage<T>> t1 = Task.Factory.StartNew(() =>
			 new ResultMessage<T> {
				 result_msg = msg,
				 result_code = 0
			 });
			return await t1;
		}

		public static ResultMessage<T> Json<T>(int code, string msg, T data = default(T)) {
			return new ResultMessage<T> {
				result_msg = msg,
				result_code = code,
				data = data
			};
		}
		public static async Task<ResultMessage<T>> JsonAsync<T>(int code, string msg, T data = default(T)) {
			Task<ResultMessage<T>> t1 = Task.Factory.StartNew(() =>
			 new ResultMessage<T> {
				 result_msg = msg,
				 result_code = code,
				 data = data
			 });
			return await t1;
		}
	}
}